package com.wili.c.asm;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Executable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import com.c.Ca;
import com.wili.c.Class9;
import com.wili.extract_srcFile.java.FieldRange;
/**
 * 
 * @author weila 2021年3月1日
 */
public abstract class AsmItems {
	public static final AsmItems EMPTY_ITEMS = new AsmItems(null) {
		@Override
		public AsmItem adde(int lineNo, AnnotatedElement ele) {
			return null;
		}
	};

	public final int id = ++GenID;

	public final List<AsmItem> list = new ArrayList<>();

	public final AsmItem fromItem;
	public AsmItems(AsmItem preItem) {
		this.fromItem = preItem;
	}

	public abstract AsmItem adde(int lineNo, AnnotatedElement ele);

	public int size() {
		return list.size();
	}
	/**
	 * 	元素类型有：属性 | 方法 | 类
	 */
	public static class AsmItem {
		public AnnotatedElement ele;

		public AsmItems dad;

		public final int index, lineNo;

		public int maxDepth = 999;

		public boolean isFieldDefine = false;

		public FieldRange fieldRange;

		public AsmItem(AsmItems dad, AnnotatedElement ele, int lineNo) {
			this.dad = dad;
			this.lineNo = lineNo;
			this.index = dad == null ? -1 : dad.size();
			this.ele = ele;
			if (ele instanceof Field) {
				Field fd = (Field) ele;
				if (fd.getName().equals("UseLog"))
					Ca.pause();
			} else if (ele instanceof Method) {
				Method md = (Method) ele;
				if (md.getName().equals("contains"))
					Ca.pause();
			}
		}

		public Class<?> getCls() {
			if (ele instanceof Executable) {
				Executable method = (Executable) ele;
				return method.getDeclaringClass();
			} else if (ele instanceof Field) {
				Field method = (Field) ele;
				return method.getDeclaringClass();
			} else {
				return ((Class<?>) ele);
			}
		}

		String toString00() {
			String name = ele.toString();
			if (ele instanceof Executable) {
				Executable method = (Executable) ele;
				//method.getDeclaringClass().getSimpleName() 
				name = Class9.strOfMethod(method);
			} else if (ele instanceof Field) {
				Field method = (Field) ele;
				name = method.getDeclaringClass().getSimpleName() + "." + method.getName();
			} else {
				name = ((Class<?>) ele).getSimpleName();
			}
			return index + ",NL:" + lineNo + "," + name;
		}
	}
	static int GenID;
}
